/*
 * @Author: zhang zhijiang 2824735304@qq.com
 * @Date: 2025-04-25 14:03:33
 * @LastEditors: zhang zhijiang 2824735304@qq.com
 * @LastEditTime: 2025-04-25 21:41:36
 * @FilePath: /linux/tcpcalculator/servercal.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include<iostream>
#include"protocol.hpp"
class calculator
{
    public:
    calculator()
    {}
    ~calculator()
    {}
    enum
    {
        deverr = 1,
        moderr,
        othererr
    };
    response calhelper(request re)
    {
        response rp(0,0);
        switch (re.op)
        {
        case '+':
        rp.result = re.x+re.y;
                    break;
        case '-':
        rp.result = re.x-re.y;
        break;
        case '*':
        rp.result = re.x*re.y;
        break;
        case '/':
        if(re.y==0)
        {
            rp.result = 0;
            rp.code = deverr;
        }
        else
        {
            rp.result = re.x/re.y;
        }
        break;
        case '%':
        if(re.y==0)
        {
            rp.result = 0;
            rp.code = moderr;
        }
        else{
            rp.result = re.x%re.y;
        }
        break;
        default:
        rp.code = othererr;
            break;
        }
        return rp;
    }
    std::string  calculate(std::string& package)
    {
        request re;
        std::string content;
        bool r=Decode(package,content);
        
        if (!r)return "";
        bool q= re.deserialize(content) ;
        if (!q)return "";
        response rp = calhelper(re);
        std::string out;
        
        rp.serialize(out);
        out = Encode(out);
        return out;
    }
};